package com.myway.components.checkIn.command;

import com.myway.utils.RegexUtil;
import com.google.gson.Gson;

public class SeatMapHelper {
	
	public static void main(String[] args) {
		String a= "J AC  HK J\\n 1..\u003d\u003d..1 \\n 2*.\u003d\u003d.*2 \\n%W ABC  HJK W\\n31...\u003d\u003d...13\\n32***\u003d\u003d***23\\n33***\u003d\u003d***33\\n34***\u003d\u003d***43\\n%Y ABC  HJK Y\\n35...\u003d\u003d...53\\n36...\u003d\u003d...63\\n37...EE...73\\n38...EE...83\\n39...\u003d\u003d.*.93\\n40*..\u003d\u003d..*04\\n41***\u003d\u003d*..14\\n42**.\u003d\u003d***24\\n43***\u003d\u003d***34\\n44***\u003d\u003d***44\\n     \u003d\u003d     \\n45***\u003d\u003d***54\\n46***\u003d\u003d***64\\n47***\u003d\u003d***74\\n48***\u003d\u003d***84\\n49***\u003d\u003d***94\\n50***\u003d\u003d***05\\n51***\u003d\u003d***15\\n52...\u003d\u003d***25\\n53...\u003d\u003d...35\\n54...\u003d\u003d...45\\n%";
		String[][] b = SeatMapHelper.getSeatPostionData(a);
		Gson  gson =new Gson();
		System.out.println(gson.toJson(b));
	}


	private final static String EU_A320=
			  "Y ABC  DEF Y\\n"
			+ " 1...==...1 \\n"
			+ " 2...==...2 \\n"
			+ " 3..*==**.3 \\n"
			+ " 4.**==.*.4 \\n"
			+ " 5.**==**.5 \\n"
			+ " 6***==***6 \\n"
			+ " 7***==***7 \\n"
			+ " 8***==***8 \\n"
			+ " 9...==.**9 \\n"
			+ "     ==     \\n"
			+ "10***==***01\\n"
			+ "11***==***11\\n"
			+ "12...EE...21\\n"
			+ "13...EE...31\\n"
			+ "14***==..*41\\n"
			+ "15***==***51\\n"
			+ "16***==***61\\n"
			+ "17***==***71\\n"
			+ "18***==***81\\n"
			+ "19***==***91\\n"
			+ "20***==***02\\n"
			+ "     ==     \\n"
			+ "21***==***12\\n"
			+ "22***==***22\\n"
			+ "23***==***32\\n"
			+ "24***==***42\\n"
			+ "25***==***52\\n"
			+ "26***==***62\\n"
			+ "27***==.**72\\n"
			+ "28...==...82\\n"
			+ "29...==...92\\n%";
	
	private final static String A320= 
			  "J AC  HK J\\n"
			+ " 1..==..1 \\n"
			+ " 2*.==.*2 \\n%"
			+ "W ABC  DEF W\\n"
			+ "31...==...13\\n"
			+ "32***==***23\\n"
			+ "33***==***33\\n"
			+ "34***==***43\\n%"
			+ "Y ABC  HJK Y\\n"
			+ "35...==...53\\n"
			+ "36...==...63\\n"
			+ "37...EE...73\\n"
			+ "38...EE...83\\n"
			+ "39...==.*.93\\n"
			+ "40*..==..*04\\n"
			+ "41***==*..14\\n"
			+ "42**.==***24\\n"
			+ "43***==***34\\n"
			+ "44***==***44\\n"
			+ "     ==     \\n"
			+ "45***==***54\\n"
			+ "46***==***64\\n"
			+ "47***==***74\\n"
			+ "48***==***84\\n"
			+ "49***==***94\\n"
			+ "50***==***05\\n"
			+ "51***==***15\\n"
			+ "52...==***25\\n"
			+ "53...==...35\\n"
			+ "54...==...45\\n%";
	
	
	/***
	 * @param seatPostion 座位位置信息
	 * @return 二维数组桌位图
	 */
	public static String[][] getSeatPostionData(String seatPostion){
		/***生成二维数组 begin***/
		String result=seatPostion.replaceAll("%", "");//去除【%结束语】
		String[] splits= result.split("\\\\n");
		int row=splits.length;
		int col=splits[0].length();
		int lastCol=splits[row-1].length();
		String[][] postion =new String[row][col];
		
		/***生成二维数组 begin***/
		if(col==lastCol){
			postion=economyClass(result,postion,row,col);
		}else{
			postion=firstClass(result,postion,row,col);
		}
		/***生成二维数组 end***/
		
		/***添加【%结束语】 begin***/
		splits= seatPostion.split("\\\\n");
		for (int i = 0; i < splits.length; i++) {
			if(splits[i].lastIndexOf("%")>-1){
				if(splits.length!=i+1){
					postion[i][0]="%"+postion[i][0];
				}
			}
		}
		/***添加【%结束语】 end***/
		
		/***添加坐标  begin***/
		String [] vals=postion[0];//舱位标示 例如：Y ABC  HJK Y
		for (int i = 0; i < postion.length; i++) {
			String x="";//x坐标
			for (int j = 0; j < postion[i].length; j++) {
				if(i!=0){
					String each= postion[i][j].trim();
					if(each.indexOf("%")>-1){ //更新舱位标示
						vals=postion[i];
					}else{
						if(isNumeric(each)){//判断是否为座位数字
							x=each;
						}else{
							//去除 【=过道】【E紧急出口】 空格
							if(!"".equals(x)&&!"".equals(each)&&!"E".equals(each)&&!"=".equals(each)){
								//添加坐标信息
								postion[i][j]=each+"@"+x+vals[j];
							}
						}
					}
				}
			}
		}
		/***添加坐标  end***/
		
		/***修改座位号  begin***/
		for (int i = 0; i < postion.length; i++) {
			int index= postion[i].length/2;
			String first="";
			for(int j=0;j<postion[i].length;j++){
				String val= postion[i][j];
				if(RegexUtil.isInteger(val)){
					if(j<index){
						first=val;
					}else if(j>index){
						postion[i][j]=first;
					}
				}
			}
			
		}
		/***修改座位号      end***/
		
		return postion;
	}
	
	
	/***
	 * 适用于经济机型，不带有头等舱
	 * @param result 桌位位置信息
	 * @param postion 座位二维数组空值
	 * @param row 纵向
	 * @param col 横向
	 * @return 二维数组桌位图
	 */
	private static String[][] economyClass(String result,String[][] postion,int row,int col){
		/***生成二维数组 begin***/
		for(int i=0;i<row;i++){
			String tempostion=result.split("\\\\n")[i]; 
			for(int j=0;j<col;j++){
				String val=tempostion.substring(j, j+1);
				postion[i][j]=val;
				if(j>0){
					String lastVal=postion[i][j-1]; //前一个座位值
					if(j>0&&isNumeric(val)&&isNumeric(lastVal)){ //判断是否为数字，拼写座位号，例如：23排是由2+3构成
						if(j<(col/2)){ //飞机左边的位置
							postion[i][j-1]=" ";
							postion[i][j]=lastVal+val;
						}else{ 			//飞机右边的位置
							postion[i][j-1]=lastVal+val;
							postion[i][j]=" ";
						}
					}
				}
				
			}
		}
		/***生成二维数组 end***/
		return postion;
	}
	
	
	/***
	 * 适用于商务机型，带有头等舱
	 * @param result 桌位位置信息
	 * @param postion 座位二维数组空值
	 * @param row 纵向
	 * @param col 横向
	 * @return 二维数组桌位图
	 */
	private static String[][] firstClass(String result,String[][] postion,int row,int col){
		/***生成二维数组 begin***/
		for(int i=0;i<row;i++){
			String tempostion=result.split("\\\\n")[i]; 
			int filling=0;
			if(col<tempostion.length()){	//补位操作,处理舱位号的问题
				filling=(tempostion.length()-col)/2;
			}
			for(int j=0;j<col;j++){
				String val="";
				if(filling>0){
					if(j==0){
						val=tempostion.substring(j, j+1+filling);
					}else if(j==tempostion.length()-filling*2-1){
						val=tempostion.substring(j+filling, j+2+filling);
					}else{
						val=tempostion.substring(j+filling, j+1+filling);
					}
				}else{
					val=tempostion.substring(j, j+1);
				}
				if("  ".equals(val)){
					val=" ";
				}
				postion[i][j]=val;
			}
		}
		/***生成二维数组 end***/
		return postion;
	}
	

	/***
	 * 判断字段是否为数字
	 * @param str
	 * @return
	 */
	private final static boolean isNumeric(String str) {
		if (str != null && !"".equals(str.trim())){
			return str.trim().matches("^[0-9]*$");
		}else{
			return false;
		}
	}
	
	/**
	 * 座位图由横向试图转为纵向，顺时针旋转90度
	 * @param postion
	 * @return
	 */
	public static String[][] rotate90Degrees(String[][] postion){
		String[][] p=new String[postion[0].length][postion.length];
		for(int i=0;i<postion[0].length;i++){
			for(int j=0;j<postion.length;j++){
				p[i][j]=postion[postion.length-1-j][i];
			}
		}
		return p;
	}
}
